from django.core.paginator import Paginator
from django.db.models import Q
from django.http import JsonResponse
from django.views.decorators.http import require_POST, require_GET

from utils import response_code
from .models import Clazz
from utils.customer_util import CustomerUtil
from utils.response_code import ResponseCode


# Create your views here.

@require_POST
def clazz_page(request):
    """班级信息分页"""
    params = CustomerUtil.get_request_params(request)
    page = params.get('page', 1)
    size = params.get('size', 5)
    # 搜索关键词  clazz  headmaster  state
    clazz = params.get('clazz')
    headmaster = params.get('headmaster')
    state = params.get('state')
    print(clazz, headmaster, state)
    #  构建空条件
    condition = Q()
    if clazz.strip():
        condition &= Q(name__contains=clazz)
    if headmaster.strip():
        condition &= Q(headmaster__contains=headmaster)
    if state.strip():
        condition &= Q(state=state)

    clazzs = Clazz.objects.filter(condition).order_by('-id')
    paginator = Paginator(clazzs, size)
    total = paginator.count
    print(total)
    current_page = paginator.page(page)
    current_page_data = current_page.object_list
    print(current_page_data)
    #  state
    clazz_list = []
    for clazz in current_page_data:
        print(clazz)
        item = {}
        item['id'] = clazz.id
        item['name'] = clazz.name
        item['state'] = clazz.get_state_display()
        item['headmaster'] = clazz.headmaster
        item['clazz_time'] = clazz.class_time
        item['class_room'] = clazz.class_room
        item['student_number'] = clazz.student_number
        clazz_list.append(item)

    response = {
        "total": total,
        "table_data": clazz_list,
        "code": ResponseCode.BUSINESS_SUCCESS
    }
    return CustomerUtil.response_json(response)


@require_POST
def clazz_insert_edit(request):
    params = CustomerUtil.get_request_params(request)
    try:
        if params.get('id'):
            Clazz.objects.filter(id=params.get('id')).update(**params)
        else:
            Clazz(**params).save()
        response = {
            'code': ResponseCode.BUSINESS_SUCCESS,
            "message": '操作成功'
        }
    except Exception as e:
        response = {
            'code': ResponseCode.BUSINESS_ERROR,
            "message": str(e)
        }

    return CustomerUtil.response_json(response)


@require_GET
def clazz_one(request, id):
    clazz = Clazz.objects.filter(id=id).values().first()
    return CustomerUtil.response_json({
        'code': ResponseCode.BUSINESS_SUCCESS,
        "message": '获取班级详情',
        "clazz": clazz
    })


@require_GET
def clazz_remove(request, id):
    try:
        clazz = Clazz.objects.filter(id=id).delete()
        response = {
            'code': ResponseCode.BUSINESS_SUCCESS,
            "message": '删除成功',
            "clazz": clazz
        }
    except Exception as e:
        response = {
            'code': ResponseCode.BUSINESS_ERROR,
            "message": '数据被占用不能删除',
        }
    return CustomerUtil.response_json(response)


@require_GET
def clazz_list(request):
    clazzs = list(Clazz.objects.all().values().order_by('-id'))
    return CustomerUtil.response_json({
        'code': ResponseCode.BUSINESS_SUCCESS,
        "clazzs": clazzs,
    })
